{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "文本特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>I</th>\n",
       "      <th>a</th>\n",
       "      <th>an</th>\n",
       "      <th>apple</th>\n",
       "      <th>have</th>\n",
       "      <th>pen</th>\n",
       "      <th>pineapple</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   I  a  an  apple  have  pen  pineapple\n",
       "0  1  1   1      1     1    1          0\n",
       "1  1  1   0      0     1    1          1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "import pandas as pd\n",
    "\n",
    "document1 = \"I have a pen, I have an apple, apple pen.\"\n",
    "document2 = \"I have a pen, I have pineapple, pineapple pen.\"\n",
    "\n",
    "cv = CountVectorizer(lowercase=False, token_pattern='\\w+', binary=True)\n",
    "model = cv.fit_transform([document1, document2])\n",
    "pd.DataFrame(model.toarray(), columns=cv.get_feature_names_out())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "import pandas as pd\n",
    "\n",
    "document1 = \"I have a pen, I have an apple, apple pen.\"\n",
    "document2 = \"I have a pen, I have pineapple, pineapple pen.\"\n",
    "\n",
    "cv = CountVectorizer(lowercase=False, token_pattern='\\w+')\n",
    "model = cv.fit_transform([document1, document2])\n",
    "pd.DataFrame(model.toarray(), columns=cv.get_feature_names_out())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>I</th>\n",
       "      <th>a</th>\n",
       "      <th>an</th>\n",
       "      <th>apple</th>\n",
       "      <th>have</th>\n",
       "      <th>pen</th>\n",
       "      <th>pineapple</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.165571</td>\n",
       "      <td>0.082785</td>\n",
       "      <td>0.140168</td>\n",
       "      <td>0.280335</td>\n",
       "      <td>0.165571</td>\n",
       "      <td>0.165571</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.192561</td>\n",
       "      <td>0.096281</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.192561</td>\n",
       "      <td>0.192561</td>\n",
       "      <td>0.326035</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          I         a        an     apple      have       pen  pineapple\n",
       "0  0.165571  0.082785  0.140168  0.280335  0.165571  0.165571   0.000000\n",
       "1  0.192561  0.096281  0.000000  0.000000  0.192561  0.192561   0.326035"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "import pandas as pd\n",
    "\n",
    "document1 = \"I have a pen, I have an apple, apple pen.\"\n",
    "document2 = \"I have a pen, I have pineapple, pineapple pen.\"\n",
    "\n",
    "tv = TfidfVectorizer(lowercase=False, token_pattern='\\w+',\n",
    "                     norm='l1', smooth_idf=False)\n",
    "model = tv.fit_transform([document1, document2])\n",
    "pd.DataFrame(model.toarray(), columns=cv.get_feature_names_out())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
